package com.sfang.common.util.file;


import com.sfang.common.base.collection.CustomKeyValuePair;
import com.sfang.common.base.collection.QuaternityTuple;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.util.*;
import java.util.function.Consumer;
import java.util.stream.Collectors;

public class FileUtil {

    static Map COLUMEN_TYPES = new HashMap() {{
        put("int", "INTEGER");
        put("tinyint", "BOOLEAN");
        put("datetime", "TIMESTAMP");
        put("varchar", "VARCHAR");
        put("text", "STRING");
        put("timestamp", "TIMESTAMP");
        put("longtext", "STRING");
        put("bigint", "BIGINT");
        put("decimal", "DECIMAL");
        put("smallint", "SMALLINT");
        put("date", "DATE");
        put("char", "CHAR");
        put("float", "FLOAT");
        put("double", "DECIMAL");
        put("json", "STRING");

    }};

    public static void schemas(File fileRead,
                                  File fileWrite, Consumer<String> consumer) {

        List<QuaternityTuple<String, String, String, String, String>> schemas
                = new ArrayList<>();

        try {
            BufferedReader br = new BufferedReader(new FileReader(fileRead));//构造一个BufferedReader类来读取文件
            String line = null;

            while ((line = br.readLine()) != null) {//使用readLine方法，一次读一行

                String[] values = line.split("\\t");
                schemas.add(QuaternityTuple.create(values[0], values[1], values[2], values.length > 3 ? values[3] : null, null));
            }

            br.close();

        } catch (Exception e) {
            e.printStackTrace();
        }


        List<CustomKeyValuePair<String,List<QuaternityTuple<String, String, String, String, String>>>> sorted
                =new ArrayList<>();

        schemas.stream()
                .collect(Collectors.groupingBy(QuaternityTuple::getFirst))
                .forEach((k, v) -> {
                    sorted.add(CustomKeyValuePair.create(k,v));
                });

        sorted.stream().sorted(Comparator.comparing(CustomKeyValuePair::getKey))
                .forEach(s->{
                    StringBuilder sb = new StringBuilder();

                    String k=s.getKey();
                    List<QuaternityTuple<String, String, String, String, String>> v=s.getValue();

                    sb.append("CREATE TABLE " + k + " (");
                    sb.append(System.lineSeparator());

                    v.stream()
                            .sorted(Comparator.comparing(col -> {
                                if (col.getFourth() != null)
                                    return 0;
                                else
                                    return 1;
                            }))
                            .forEach(col -> {
                                sb.append(String.format("  %s %s,",
                                        col.getSecond(),
                                        COLUMEN_TYPES.get(col.getThird())));
                                sb.append(System.lineSeparator());

                            });

                    sb.append(String.format("PRIMARY KEY(%s))",
                            v.stream().filter(c -> c.getFourth() != null && c.getFourth().equalsIgnoreCase("pri")).findFirst().get().getSecond()));
                    sb.append(System.lineSeparator());

                    sb.append(String.format("PARTITION BY HASH(%s) PARTITIONS 16",
                            v.stream().filter(c -> c.getFourth() != null && c.getFourth().equalsIgnoreCase("pri")).findFirst().get().getSecond()));
                    sb.append(System.lineSeparator());

                    sb.append(" STORED AS KUDU;");
                    sb.append(System.lineSeparator());
                    sb.append(System.lineSeparator());

                    consumer.accept(sb.toString());

                });

        /*BufferedWriter bw = null;
        try {
            bw = new BufferedWriter(new OutputStreamWriter(
                    new FileOutputStream(fileWrite), "UTF-8"));
            bw.write(sb.toString());
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (bw != null) {
                try {
                    bw.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }*/

    }
}
